import { useLocalStorage } from "@vueuse/core";
import { defineStore } from "pinia";
import { changeLocale as changeLocaleUtil } from "@/locale";
import pinia from "@/store";
import { computed } from "vue";

export const useLocaleStore = defineStore("Locale", () => {
  // 缓存中的语言
  const locale = useLocalStorage<string>("LOCALE", "zh-CN");

  // 外部获取当前语言
  const getLocale = computed(() => locale.value);

  /**
   * 改变语言
   * @param lang
   */
  const changeLocale = async (lang: string) => {
    await changeLocaleUtil(lang);
    locale.value = lang;
  };

  return {
    getLocale,
    changeLocale,
  };
});

export const useLocaleStoreWithout = () => {
  return useLocaleStore(pinia);
};
